﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 朝着主角的装置
/// 攻击状态时面朝主角，其他状态则不朝
/// </summary>
public class LaserE : Entity{

#region 基础的获取控件字段
    private Transform tf;//获取自身
    private FSMSystem selfFSM;//获取滋生的状态机系统
    #endregion
#region 一些基本数据
    private bool isAiming = false;//是否处于瞄准状态
#endregion

    // Use this for initialization
    void Start()
    {
        tf = transform;
        selfFSM = GetComponent<FSMSystem>();
        selfFSM.OnStateChange += Judge;
    }
	// Update is called once per frame

	void Update () {
        if(isAiming)
        if (GameManager.Instance.Sui!=null)
        {
            Turn(GameManager.Instance.Sui.transform.position, tf);
        }
    }
    

    /// <summary>
    /// 状态装换函数
    /// </summary>
    /// <param name="oldState"></param>
    /// <param name="newState"></param>
    private void Judge(FSMState oldState,FSMState newState)
    {
        if (newState.Id==StateID.Attack)
        {
            isAiming = true;
        }
        else
        {
            isAiming = false;
        }
    }


    //跟随主角转动
    private void Turn(Vector3 target,Transform self)
    {
        Vector3 point1 = Vector3.Project(self.localPosition - target, self.TransformDirection(Vector3.forward));
        Vector3 point2 = point1 + target;
        float angle1 = Mathf.Rad2Deg * Mathf.Atan((self.position.y - point2.y) / (self.position.x - point2.x));
        if (self.position.x - point2.x < 0)
            angle1 = angle1 - 90;
        else
            angle1 = angle1 + 90;
        self.localEulerAngles = new Vector3(0, 0, angle1);
    }

}

    
